﻿package mortal.game.view.copy.towerCopy.panel
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.towerCopy.*;
    import mortal.game.view.copy.towerCopy.cellrender.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TowerResultPanel extends Window
    {
        private var _bg:GImageBitmap;
        private var _wumen:GImageBitmap;
        private var _stateBg:GBitmap;
        private var _state:GBitmap;
        private var _txt1:GTextFiled;
        private var _txt2:GTextFiled;
        private var _award:GTileList;
        private var _outBtn:SecTimerButton;
        private static var _instance:TowerResultPanel;

        public function TowerResultPanel(param1:ILayer = null)
        {
            super(param1);
            setSize(640, 294);
            return;
        }// end function

        public function setReward() : Array
        {
            var _loc_4:ItemPlayer = null;
            var _loc_5:String = null;
            var _loc_6:SPlayerItem = null;
            var _loc_7:ItemData = null;
            var _loc_1:* = ThingUtil.dropItemUtil.dropItemMap.map;
            var _loc_2:* = new Dictionary();
            var _loc_3:Array = [];
            for each (_loc_4 in _loc_1)
            {
                
                _loc_6 = _loc_4.dropEntityInfo.playerItems[0] as SPlayerItem;
                if (_loc_6)
                {
                    _loc_2[_loc_6.itemCode] = int(_loc_2[_loc_6.itemCode]) + _loc_6.itemAmount;
                    continue;
                }
                _loc_2[_loc_4.dropEntityInfo.unit] = int(_loc_2[_loc_4.dropEntityInfo.unit]) + _loc_4.dropEntityInfo.cost;
            }
            for (_loc_5 in _loc_2)
            {
                
                _loc_7 = new ItemData(int(_loc_5));
                _loc_7.extData = _loc_2[_loc_5];
                if (int(_loc_5) < 100)
                {
                    _loc_7.itemAmount = int(_loc_2[_loc_5]);
                }
                _loc_3.push(_loc_7);
            }
            if (this.isDisposed || this._award == null)
            {
                return _loc_3;
            }
            this._award.dataProvider = new DataProvider(_loc_3);
            this._award.drawNow();
            return _loc_3;
        }// end function

        public function showRewards(param1:Array) : void
        {
            if (this._award == null || param1 == null)
            {
                return;
            }
            this._award.dataProvider = new DataProvider(param1);
            this._award.drawNow();
            return;
        }// end function

        private function setData() : void
        {
            var _loc_1:* = Cache.instance.copy.tower;
            if (_loc_1 == null || _loc_1.towerCopyInfo == null || Cache.instance.copy == null || Cache.instance.copy.copyInfo == null)
            {
                if (_loc_1 && _loc_1.towerCopyInfo && _loc_1.towerCopyInfo.result == false)
                {
                    this._outBtn.x = 335;
                    this._outBtn.label = "" + Language.getString(70999);
                    this._txt1.text = Language.getString(70701);
                    this._txt2.text = Language.getString(70702);
                    this._stateBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultFailureBg);
                    this._state.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultFailure);
                }
                else
                {
                    this.hide();
                }
                return;
            }
            if (_loc_1.towerCopyInfo.result)
            {
                if (_loc_1.getIsMaxLv(Cache.instance.copy.copyInfo.code, _loc_1.towerCopyInfo.level))
                {
                    this._outBtn.x = 335;
                }
                else
                {
                    this._outBtn.x = 280;
                }
                this._txt1.text = Language.getStringByParam(70700, _loc_1.towerCopyInfo.level);
                this._txt2.text = "";
                this._stateBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultWinBg);
                this._state.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultWin);
            }
            else
            {
                this._outBtn.x = 335;
                this._outBtn.label = "" + Language.getString(70999);
                this._txt1.text = Language.getString(70701);
                this._txt2.text = Language.getString(70702);
                this._stateBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultFailureBg);
                this._state.bitmapData = GlobalClass.getBitmapData(ImagesConst.TowerResultFailure);
            }
            return;
        }// end function

        private function onOut(event:MouseEvent) : void
        {
            if (this._outBtn.label == Language.getString(70999))
            {
                this.hide();
                return;
            }
            if (Cache.instance.copy.tower.isDropEnd)
            {
                this.hide();
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyClickQuickBtn));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TowerShow));
            }
            return;
        }// end function

        private function onNext(event:MouseEvent = null) : void
        {
            var _loc_2:TCopy = null;
            if (Cache.instance.copy.tower.isDropEnd)
            {
                _loc_2 = Cache.instance.copy.copyInfo;
                if (_loc_2)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TowerEnterCopy, _loc_2.code));
                }
            }
            return;
        }// end function

        private function onResCompl() : void
        {
            this.setData();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap("", 0, 0, this);
            LoaderHelp.setBitmapdata("ChallengPassBg.swf", this._bg);
            this._wumen = UIFactory.gImageBitmap("", -150, -125, this);
            LoaderHelp.setBitmapdata("ChallengPassBgWumen.swf", this._wumen);
            this._stateBg = UIFactory.gBitmap("", 90, -200, this);
            this._state = UIFactory.gBitmap("", 307, -120, this);
            this._txt1 = UIFactory.gTextField("", 135, 100, 500, 20, this, GlobalStyle.textFormatBai.center());
            this._txt2 = UIFactory.gTextField("", 135, 183, 500, 20, this, GlobalStyle.textFormatBai.center());
            this._award = UIFactory.tileList(299, 173, 225, 60, this);
            this._award.setStyle("cellRenderer", TowerRewardsCellRenderer);
            this._award.columnWidth = 45;
            this._award.rowHeight = 45;
            this._award.direction = GBoxDirection.HORIZONTAL;
            this._outBtn = UIFactory.secTimerButton("" + Language.getString(71000), 280, 250, 92, 32, this);
            pushUIToDisposeVec(UIFactory.bg(150, 230, 500, 2, this, ImagesConst.SplitLine));
            pushUIToDisposeVec(UIFactory.bg(150, 150, 500, 2, this, ImagesConst.SplitLine));
            this._outBtn.configEventListener(MouseEvent.CLICK, this.onOut);
            _closeBtn.parent.addChild(_closeBtn);
            LoaderHelp.addResCallBack(ResFileConst.towerResultPanel, this.onResCompl);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.towerResultPanel, this.onResCompl);
            this._outBtn.stop();
            this._bg.dispose(param1);
            this._bg = null;
            this._stateBg.dispose(param1);
            this._stateBg = null;
            this._state.dispose(param1);
            this._state = null;
            this._txt1.dispose(param1);
            this._txt1 = null;
            this._txt2.dispose(param1);
            this._txt2 = null;
            this._award.dispose(param1);
            this._award = null;
            this._outBtn.dispose(param1);
            this._outBtn = null;
            this._wumen.dispose(param1);
            this._wumen = null;
            return;
        }// end function

        public static function get instance() : TowerResultPanel
        {
            if (!_instance)
            {
                _instance = new TowerResultPanel;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
